package com.ja.openchat.viewmodels;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.ja.openchat.ui.chat.group.adapter.GroupSettingChildAdapter;
import com.ja.openchat.ui.chat.group.adapter.GroupSettingOfMembersAdapter;
import com.zy.openchat.core.manager.TgEvent;
import com.zy.openchat.core.model.file.FileInfo;
import com.zy.openchat.core.model.file.FileTaskInfo;
import com.zy.openchat.core.model.message.ChatInfo;
import com.zy.openchat.core.model.message.MessageInfo;

import me.goldze.mvvmhabit.base.BaseViewModel;


public class ContactDetailChildViewModel extends BaseViewModel {

    private ChatInfo conversation;
    private GroupSettingChildAdapter messageAdapter;
    private GroupSettingOfMembersAdapter groupSettingOfMembersAdapter;

    public ContactDetailChildViewModel(@NonNull Application application) {
        super(application);
    }



    public void onTgEvent(final TgEvent tg_event){
        if(tg_event==null){
            return;
        }

        switch (tg_event.event) {
            case EUser_Td_Contact_Photo_Ok:
                if(groupSettingOfMembersAdapter!=null){
                    groupSettingOfMembersAdapter.notifyDataSetChanged();
                }
                break;
            case EUser_Td_Message_Photo_Ok:
            case EUser_Td_Message_Video_Ok:
            case EUser_Td_Message_Audio_Ok:
            case EUser_Td_Message_Voice_Ok:
            case EUser_Td_Message_Preview_Photo_Ok:
            case EUser_Td_Message_Document_Ok:
            case EUser_Td_Message_Animation_Ok:
                try {
                    if(messageAdapter!=null&&tg_event.data instanceof FileTaskInfo &&tg_event.dataEx instanceof FileInfo){
                        FileInfo fileInfo = (FileInfo) tg_event.dataEx;
                        String ids = ((FileTaskInfo)tg_event.data).id;
                        if(!TextUtils.isEmpty(ids)&&ids.contains("_")){
                            String[] idArray = ids.split("_");
                            if(idArray.length >= 2){
                                if(idArray[0].equals(String.valueOf(conversation.id))){
                                    for(int i = 0; i< messageAdapter.getItemCount();i++){
                                        MessageInfo message = messageAdapter.getItem(i);
                                        if(message.id == Long.parseLong(idArray[1])){
                                            switch (tg_event.event) {
                                                case EUser_Td_Message_Photo_Ok:
                                                    message.updateMessagePhotoFile(fileInfo);
                                                    break;
                                                case EUser_Td_Message_Preview_Photo_Ok:
                                                    message.updatePreviewPhotoFile(fileInfo);
                                                    break;
                                                case EUser_Td_Message_Video_Ok:
                                                    message.updateVideoFile(fileInfo);
                                                    break;
                                                case EUser_Td_Message_Document_Ok:
                                                    message.updateDocumentFile(fileInfo);
                                                    break;
                                                case EUser_Td_Message_Animation_Ok:
                                                    message.updateAnimationFile(fileInfo);
                                                    break;
                                                default:
                                                    message.updateAudioFile(fileInfo);
                                                    break;

                                            }
                                            messageAdapter.notifyItemChanged(i);
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
                break;
        }
    }

    public void setAdapter(ChatInfo conversation, GroupSettingChildAdapter messageAdapter) {
        this.conversation = conversation;
        this.messageAdapter = messageAdapter;
    }

    public void setgroupSettingOfMembersAdapter(GroupSettingOfMembersAdapter groupSettingOfMembersAdapter) {
        this.groupSettingOfMembersAdapter = groupSettingOfMembersAdapter;
    }
}
